package huisu.onClass.class02.exer03;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int[][] mat = new int[m][m];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                mat[i][j] = sc.nextInt();
            }
        }
        boolean[][] flag = new boolean[m][m];
        int count = 0;
        int[][] nextPos = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                if (mat[i][j] == 1 && !flag[i][j]) {
                    count++;
                    dfs(i, j, m, m, flag, mat, nextPos);
                }
            }
        }
        System.out.println(count);
    }

    public static void dfs(int curRow, int curCol, int rows, int cols, boolean[][] flag, int[][] mat, int[][] nextPos) {
        flag[curRow][curCol] = true;
        for (int i = 0; i < 4; i++) {
            int nextRow = curRow + nextPos[i][0];
            int nextCol = curCol + nextPos[i][1];
            if (nextRow >= 0 && nextRow < rows && nextCol >= 0 && nextCol < cols && !flag[nextRow][nextCol] && mat[nextRow][nextCol] == 1) {
                dfs(nextRow, nextCol, rows, cols, flag, mat, nextPos);
            }
        }
    }
}
